$(document).ready(function() {  
    
    positionner_plus();
    
    $(".draggable_user").draggable({
                                start: function(event, ui) {
                                    //alert(ui.helper.attr("id"));
                                }, 
                                helper:"clone"
                            });
    $(".draggable_admin").draggable({
                                start: function(event, ui) {
                                    //alert(ui.helper.attr("id"));
                                }, 
                                helper:"clone"
                            });
   
    $(".droppable_user").droppable({
                    accept: ".draggable_admin",
                    activeClass: "ui-state-hover",
                    hoverClass: "ui-state-active",
                    drop: function( event, ui ) {
                        ajax(ui.helper.attr("id"), "user");
                        //$( this ).addClass( "ui-state-highlight" );
                    }
		});
    $(".droppable_admin").droppable({
                    accept: ".draggable_user",
                    activeClass: "ui-state-hover",
                    hoverClass: "ui-state-active",
                    drop: function( event, ui ) {
                        //$( this ).addClass( "ui-state-highlight" );
                        ajax(ui.helper.attr("id"), "admin");
                    }
		});
     
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
         
        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  
     
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
               
        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);
     
        //transition effect
        $(id).fadeIn(2000); 
    });
     
    //if close button is clicked
    $('.modal_popup .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .modal_popup').hide();
    });     
     
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.modal_popup').hide();
    });   
});

function ajax(user, type){
    
    var data = "nav=changer_admins&user="+user+"&type="+type;
    $.ajax({
        type: 'POST',
        url: 'admin_order.behind.php',
        data: data,
        async: false,
        success: function(data) {
            var donnee = eval('('+ data +')');
            if(donnee == "insertionOk"){
                if(type=="admin"){
                    
                    var oldElem = $("#"+user);
                    
                    var madiv = oldElem.clone(true);
                    
                    madiv.id=oldElem.id;
                    oldElem.id = user+"_old";
                    oldElem.attr("style","display:none");
                    
                    madiv.attr("class", "div_user draggable_admin");
                    madiv.attr("style","display:block");
                    
                    $("#conteneur_admin").append(madiv);
                    
                    $(".text_info_admins").empty();
                    $(".text_info_admins").text( "Administrateur ajouté" )
                                        .fadeIn( 2000, function(){
                                                $( this ).fadeOut(2000)
                                        });
                }
            }
            else if(donnee == "suppressionOk"){
                if(type=="user"){
                    
                    var oldElem2 = $("#"+user);
                    
                    var madiv2 = oldElem2.clone(true);
                    
                    madiv2.id=oldElem2.id;
                    oldElem2.id = user+"_old";
                    oldElem2.attr("style","display:none");
                    
                    madiv2.attr("class", "div_user draggable_user");
                    madiv2.attr("style","display:block");
                    
                    $("#conteneur_user").append(madiv2);
                    
                    $(".text_info_admins").empty();
                    $(".text_info_admins").text( "Administrateur supprimé" )
                                        .fadeIn( 3000, function(){
                                                $( this ).fadeOut(3000)
                                        });
                }
            }
        }
    });
}

function add(){
    var nom_ordre = $('#nom_0').val();
    var photo = $("#hiddenPhoto_0").val();
    var data = "nav=add&name="+nom_ordre+"&photo="+photo;
    $.ajax({
        type: 'POST',
        url: 'admin_order.behind.php',
        data: data,
        async: false,
        success: function(data) {
            var donnee = eval('('+ data +')');
            if(donnee == "Ok"){
                labelModification("Ordre ajouté", false);
            }
            else{
                labelModification("Erreur lors de l'ajout de l'ordre", true);
            }
        }
    });
}

function update(order_id, order_name){
    
    if(confirm("Etes-vous sur de vouloir modifier l'ordre " + order_name + " ?")){
        var nom_ordre = $('#nom_'+order_id).val();
        var photo = $("#hiddenPhoto_"+order_id).val();
        var data = "nav=update&order="+order_id+"&name="+nom_ordre+"&photo="+photo;
        //alert(data);
        $.ajax({
            type: 'POST',
            url: 'admin_order.behind.php',
            data: data,
            async: false,
            success: function(data) {
                var donnee = eval('('+ data +')');
                if(donnee == "Ok"){
                    labelModification("Ordre mis à jour", false);
                }
                else{
                    labelModification("Erreur lors de la mise à jour de l'ordre", true);
                }
            }
        });
    }
}

function del(order_id, order_name){
    if(confirm("Etes-vous sur de vouloir supprimer l'ordre " + order_name + " ?")){
        
        var data = "nav=del&order="+order_id;
        
        $.ajax({
            type: 'POST',
            url: 'admin_order.behind.php',
            data: data,
            async: false,
            success: function(data) {
                var donnee = eval('('+ data +')');
                if(donnee == "Ok"){
                    labelModification("Ordre supprimé", false);
                    $('#order_'+order_id).fadeOut('fast');
                }
                else{
                    labelModification("Erreur lors de la suppression de l'ordre", true);
                }
            }
        });
    }
}
function del_forbidden(name){
    alert("Impossible de supprimer " + name + " car cet ordre est lié à des photographies.");
}

//--Positionne le bouton + en fonction de la taille de l'écran'
function positionner_plus(){
    var pos_div_a_pos = $('.div_add_ordre');
    var pos_div_ref = $("#div_middle").offset();
    pos_div_a_pos.attr("style", "left:" + (pos_div_ref.left + 830) + "px");
}

function add_new_order(){
    var div = $("#order_0");
    div.fadeIn(5000);
    positionScrollEnBas();
}

function new_order_visble(){
    var div = $("#order_0");
    div.fadeIn("fast");
}

function positionScrollEnBas(){
    var middle_scroll = $("#div_middle");
    
    middle_scroll.scrollTop(document.getElementById("div_middle").scrollHeight);
}

function labelModification(text, iserror){
    var lab = $('#label_error');
    if(iserror){
       lab.attr('style', 'color:red;') ;
    }
    else{
       lab.attr('style', 'color:green;') ;
    }
    lab.text(text);
    lab.fadeIn( 3000, function(){
                    $( this ).fadeOut(3000)
                });
}
 